package com.lvandroid.player.activity;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Size;
import android.view.SurfaceView;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;

import com.lvandroid.play.player.AllRoundPlayerManager;
import com.lvandroid.play.player.AllRoundPlayer;
import com.lvandroid.play.util.LogUtil;
import com.lvandroid.play.util.SizeUtil;
import com.lvandroid.player.R;

import java.util.ArrayList;
import java.util.List;

public class VideoPlayerActivity extends AppCompatActivity {

    private SurfaceView mSurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSurface = findViewById(R.id.surface);
        easyStart(getVideoFile());
        //normalStart(getVideoFile());
    }

    private void normalStart(String movie) {
        if(!TextUtils.isEmpty(movie)) {
            final AllRoundPlayer player = AllRoundPlayerManager.getInstance().createPlayer(AllRoundPlayerManager.MEDIA_TYPE_VIDEO);
            player.setDisplay(mSurface);
            player.setDataSource(movie);
            player.loopPlay(true);
            player.setPrepareListener(item -> {
                float width = item.getWidth();
                float height = item.getHeight();
                Size s = SizeUtil.calculateSizeAsRatio(mSurface.getWidth(), mSurface.getHeight(), width / height);
                updateView(s.getWidth(), s.getHeight());
                player.start();
            });

            player.prepare();
        }
    }

    private void easyStart(String movie) {
        AllRoundPlayer player = AllRoundPlayerManager.getInstance().createPlayer(AllRoundPlayerManager.MEDIA_TYPE_VIDEO);
        if(!TextUtils.isEmpty(movie)) {
            player.loopPlay(true);
            List<String> datas = new ArrayList<>();
            datas.add(movie);
            player.start(datas, mSurface);
            player.getPlayItem().save();
            /*player.setDisplay(mSurface);

            player.setPrepareListener(item -> {
                float width = item.getWidth();
                float height = item.getHeight();
                Size s = SizeUtil.calculateSizeAsRatio(mSurface.getWidth(), mSurface.getHeight(), width / height);
                updateView(s.getWidth(), s.getHeight());
            });
            player.start(movie);*/
        }
    }

    private void updateView(int w, int h) {
        ViewGroup.LayoutParams lp = mSurface.getLayoutParams();
        lp.width = w;
        lp.height = h;
        mSurface.setLayoutParams(lp);
    }

    public String getVideoFile() {
        Cursor c = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.Media.DATA}
        , null ,null, null);
        if(null != c && c.moveToNext()) {
            String path = c.getString(0);
            LogUtil.i("wyt", "getVideoFile : " + path);
            c.close();
            return path;
        }

        return null;
    }

}